<?php

namespace app\common\model\mysql;

use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;
use think\model\relation\HasOne;

class User extends BaseModel
{
    public $schema=[
        'id'=>'int',
        'name'=>'varchar',
        'phone_number'=>'varchar',
        'sex'=>'tinyint',
        'avatar'=>'varchar',
        'status'=>'tinyint',
        'autherized'=>'tinyint',
        'last_login_time'=>'int',
        'last_login_ip'=>'varchar',
        'id_card'=>'varchar',
        'open_id'=>'varchar',
        'create_time'=>'int',
        'update_time'=>'int'
    ];

    /*public function userInfo(): HasOne
    {
        return $this->hasOne(UserInfo::class,'user_id');
    }*/

    public function userAssociation(): HasOne
    {
        return $this->hasOne(UserAssociation::class,'user_id');
    }

    /**
     * 获取分页列表
     * @param $where
     * @param $page
     * @param $limit
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getTableList($where,$page,$limit): array
    {

        $aField=$this->schema;unset($aField['open_id']);
        $aField=array_keys($aField);
        $bField=['id','user_id','audit'];

        if(!empty($page)&&!empty($limit)){
            $list=$this->field($aField)->with(['userAssociation'=>function($query) use ($bField){
                $query->field($bField);
            }])->order('id desc')->where($where)->page($page,$limit)->select();
        }else{
            $list=$this->field($aField)->with(['userAssociation'=>function($query) use ($bField){
                $query->field($bField);
            }])->order('id desc')->where($where)->select();
        }
        $list=$list->isEmpty()?[]:$list->toArray();
        $count=$this->with(['userAssociation'=>function($query) use ($bField){
            $query->field($bField);
        }])->where($where)->count();
        return ['count'=>$count,'list'=>$list];
    }
}
